<?php

/**
 * Created by PhpStorm.
 * User: zsm
 * Date: 2018/5/11
 * Time: 10:48
 */
namespace Library;
class View
{
    private static $template_file;

    /**
     * 输出值到视图
     * @author zsm
     * @param $data
     * @param $view_file
     */
    public static function display($data, $view_file)
    {

        if (is_array($data)) {
            extract($data);//将值赋给键
        } else {
            throw new Exception('请输入数组');
        }

        ob_start();
        ob_implicit_flush(0);
        include self::checkTemplate($view_file);

        $content=ob_get_clean();
        echo $content;

    }

    /**
     * 验证模板文件是否存在
     * @author zsm
     * @param $view_file
     * @return string
     */
    public static function checkTemplate($view_file)
    {
        $view_path = APP_PATH .'index/view/';
        $template_name = empty($view_file) ? ACTION : $view_file;
        self::$template_file=$view_path . CONTROLLER . '/' . $template_name . Config('VEXT');
        if (file_exists(self::$template_file)){
            return self::$template_file;
        }
        //throw new Exception(self::$template_file.'文件不存在');
    }
}